package xyz.pro5books.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import xyz.pro5books.mapper.BookReportMapper;
import xyz.pro5books.pojo.BookReport;
import xyz.pro5books.pojo.User;
import xyz.pro5books.vo.PageResult;

import java.util.List;

@Service
public class BookReportServiceImpl implements BookReportService{
    @Autowired
    private BookReportMapper bookReportMapper;
    @Override
    public PageResult getBookReportList(PageResult pageResult) {
        //1.定义MP分页对象，arg1页数，arg2行数
        IPage iPage = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        //2.构建查询条件构造器
        QueryWrapper<BookReport> queryWrapper =new QueryWrapper<>();
        //判断用户的数据是否有效，有效true,无效false
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.eq(flag, "userId", pageResult.getQuery())
                .eq("settled",0);
        //经过MP的分页查询，将所有的分页数(total/结果/页数/条数/xxx)据封装到Ipage对象中
        iPage = bookReportMapper.selectPage(iPage,queryWrapper);
        //从分页对象中获取分页后的总记录数
        long total = iPage.getTotal();
        //从分页对象中获取分页后的结果
        List<User> rows = iPage.getRecords();

        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    @Transactional
    public void updateBookReportStatus(BookReport bookReport) {
        bookReportMapper.updateById(bookReport);
    }

    @Override
    public void deleteBookReportById(Integer id) {
        bookReportMapper.updateById(new BookReport().setId(id).setSettled(true));
    }

}
